//
//  SHBasicAnimaViewController.m
//  SDemo
//
//  Created by gg on 2018/8/15.
//  Copyright © 2018年 gg. All rights reserved.
//

#import "SHBasicAnimaViewController.h"

@interface SHBasicAnimaViewController ()

@property(nonatomic,strong)UIView *view1;

@end

@implementation SHBasicAnimaViewController

-(UIView *)view1 {
    if (_view1 == nil) {
        _view1 = [UIView new];
        _view1.backgroundColor = [UIColor orangeColor];
        _view1.frame = CGRectMake(20, 100, 100, 100);
    }
    return _view1;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"动画";
    self.view.backgroundColor = [UIColor whiteColor];
    
//    [self startViewAnima];
    [self startCAAnimation];
}

-(void)startViewAnima {
    [self.view addSubview:self.view1];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapClick:)];
    [self.view1 addGestureRecognizer:tap];

}

-(void)startCAAnimation {
    [self.view addSubview:self.view1];
     [self.view addSubview:self.view1];
//     UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapClick1:)];
    
    //关键帧动画
//    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapClick2:)];
//     [self.view1 addGestureRecognizer:tap];
    
    //组动画
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapClick3:)];
    [self.view1 addGestureRecognizer:tap];
    
}

-(void)tapClick:(UITapGestureRecognizer *)gesture {
    //基础动画
//    [UIView animateWithDuration:1 delay:0 options:(UIViewAnimationOptionRepeat | UIViewAnimationOptionCurveEaseInOut) animations:^{
//        self.view1.frame = CGRectMake(20, 200, 100, 100);
//    } completion:^(BOOL finished) {
//        NSLog(@"动画结束");
//    }];
    
    /**
     关键帧动画
     下面真正意义上的时间是 Duration ，回调里面的starttime 和 relativeDuration 都是相对时间
     */
    [UIView animateKeyframesWithDuration:2 delay:0.0 options:(UIViewAnimationOptionRepeat | UIViewAnimationOptionCurveEaseInOut) animations:^{
        
        [UIView addKeyframeWithRelativeStartTime:0 relativeDuration:0.5 animations:^{
            self.view1.frame = CGRectMake(20, 200, 100, 100);
        }];
        
        [UIView addKeyframeWithRelativeStartTime:0.5 relativeDuration:0.3 animations:^{
            self.view1.frame = CGRectMake(100, 200, 100, 100);
        }];
        
        [UIView addKeyframeWithRelativeStartTime:0.8 relativeDuration:1 animations:^{
            self.view1.frame = CGRectMake(20, 200, 200, 200);
        }];
    
    } completion:^(BOOL finished) {
        
    }];
}

//basic
-(void)tapClick1:(UITapGestureRecognizer *)gesture {
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position.y"];
    animation.duration = 1;
    animation.toValue = @300;
    animation.beginTime = CACurrentMediaTime() + 0.5;
    //下面是两个属性设置后，就会保持做完动画的状态
    animation.fillMode = kCAFillModeForwards;
    animation.removedOnCompletion = NO;
    [self.view1.layer addAnimation:animation forKey:@""];
}


// keyframe
- (void)tapClick2:(UITapGestureRecognizer *)gesture {
//    CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"transform.rotation"];
//    NSNumber *value1 = [NSNumber numberWithFloat:-M_PI/180 *4];
//    NSNumber *value2 = [NSNumber numberWithFloat:M_PI/180 *4];
//    NSNumber *value3 = [NSNumber numberWithFloat:-M_PI/180 *4];
//    animation.values = @[value1,value2,value3];
//    animation.repeatCount = MAXFLOAT;
//    [self.view1.layer addAnimation:animation forKey:@""];
    
    CAKeyframeAnimation *key = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(self.view1.center.x,self.view1.center.y , 200, 200)];
    key.path = path.CGPath;
    key.duration = 3.0f;
    key.repeatCount = MAXFLOAT;
    [self.view1.layer addAnimation:key forKey:nil];
 }


-(void)tapClick3:(UITapGestureRecognizer *)gesture {
    CAAnimationGroup *group = [CAAnimationGroup animation];
    group.duration = 4.0;
    CABasicAnimation *basic1 = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
    basic1.toValue = @2.0;
    basic1.duration = 1.0;
    
    CABasicAnimation *basic2 = [CABasicAnimation animationWithKeyPath:@"position.x"];
    basic2.toValue = @400;
    basic2.duration = 3.0;
    [group setAnimations:@[basic1,basic2]];

    [self.view1.layer addAnimation:group forKey:nil];
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
